define(
	[
		'require',
		'./detail_nodedetail_overview_ctrl',
		'./detail_nodedetail_type_ctrl',
		'./detail_nodedetail_ctrl'
	],
	function(require){
		var Controller = require('./detail_nodedetail_overview_ctrl').extend({
			needs: ["businessDetailNodedetailType"],

			init: function(){
				if(overviewOrCenterSwitch == "business"){
					this.set("defaultSelect","business/response_time/avg");
				}else{
					this.set("defaultSelect","system/response_time/avg");
				}	
			},

			
			requireIndicators: function(protocol){
				var ctrl = this,
					appId = this.get("business.id"),
					resourceId = this.get("model.id");
				this.set("indicators.isLoading", true);

				return this.store.ajax("GET", "keytrades/indicators", {
					appId: appId,
					tradeId: resourceId,
					panelId: "business"

				})
				.then(function(response){
					ctrl.set("indicators", Ember.A(response.indicatorConfig));
					ctrl.set("indicators.isLoading", false);
					return response;
				});
			},

			requirePerformances: function() {
				var ctrl = this,
					appId = this.get("business.id"),
					serviceId = this.get("node.id"),
					resourceId = this.get("model.id"),
					appPfms = this.get("business.performances"),
					store = this.store,
					pfmsCompletion = ctrl.get("controllers.businessCenter").performancesCompletion,
					typeServerId = this.get("model.applicationId")+'-'+this.get("model.belongTo");


				if( !(appPfms && appPfms.length > 0) ){
					return Ember.RSVP.reject("时序图没有数据");
				}

				var sDate = appPfms[0].timestamp;
				var sDate0, eDate0;
				sDate0 =  new Date(sDate);
				eDate0 =  new Date(+sDate0);
				eDate0.setHours(eDate0.getHours()+1);

				return store.ajax("GET", "performances/transaction/"+resourceId, {
					appId: appId,
					resId: resourceId,
					serviceId: typeServerId,
					type:'resource',
					rows: 5,
					startDate: +sDate0,
					endDate: +eDate0
				})
				.then(function(response){
					var data = pfmsCompletion(Ember.A(response["performances"]||[]), sDate0, eDate0);
					ctrl.set("performances", data);
				});
			},

			requireCompareData: function() {
				var ctrl = this,
					appId = this.get("business.id"),
					serviceId = typeServerId = this.get("model.applicationId")+'-'+this.get("model.belongTo"),
					resourceId = this.get("model.id"),
					appPfms = this.get("business.performances"),
					store = this.store,
					pfmsCompletion = ctrl.get("controllers.businessCenter").performancesCompletion;

				if( !(appPfms && appPfms.length > 0) ){
					return Ember.RSVP.reject("时序图没有数据");
				}

				var sDate = appPfms[0].timestamp;
				var sDate0, eDate0, sDate1, eDate1, sDate2, eDate2;

				sDate0 =  new Date(sDate);
				eDate0 =  new Date(+sDate0);
				eDate0.setHours(eDate0.getHours()+1);

				sDate1 = new Date(sDate);
				sDate1.setDate(sDate1.getDate()-1);
				eDate1 = new Date(+sDate1);
				eDate1.setHours(eDate1.getHours()+1);

				sDate2 = new Date(sDate);
				sDate2.setDate(sDate2.getDate()-7);
				eDate2 = new Date(+sDate2);
				eDate2.setHours(eDate2.getHours()+1);

				return Ember.RSVP.all([
					store.ajax("GET", "performances/transaction/"+resourceId, {
						appId: appId,
						resId: resourceId,
						serviceId: serviceId,
						type:'resource',
						rows: 5,
						startDate: +sDate0,
						endDate: +eDate0
					}),
					store.ajax("GET", "performances/transaction/"+resourceId, {
						appId: appId,
						serviceId: serviceId,
						resId: resourceId,
						type:'resource',
						rows: 5,
						startDate: +sDate1,
						endDate: +eDate1
					}),
					store.ajax("GET", "performances/transaction/"+resourceId, {
						appId: appId,
						serviceId: serviceId,
						resId: resourceId,
						type:'resource',
						rows: 5,
						startDate: +sDate2,
						endDate: +eDate2
					})
				])
				.then(function(response){
					var tradePerformances = [
						pfmsCompletion(Ember.A(response[2]["performances"]||[]), sDate2, eDate2),
						pfmsCompletion(Ember.A(response[1]["performances"]||[]), sDate1, eDate1),
						pfmsCompletion(Ember.A(response[0]["performances"]||[]), sDate0, eDate0)
					];
					ctrl.set("nodePerformances", tradePerformances);
				});
			}
		});

		APM.BusinessDetailNodetypeOverviewtypeController = Controller;

		return Controller;
	}
);	
